package com.tanhua.dubbo.api.impl.mongo;

import com.mongodb.client.result.UpdateResult;
import com.tanhua.domain.mongo.*;
import com.tanhua.dubbo.api.mongo.QuestionnaireApi;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.List;


@Service
public class QuestionnaireApiImpl implements QuestionnaireApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    //查询问卷类型
    @Override
    public List<Questionnaire> queryQuestionnaire() {
        List<Questionnaire> list = mongoTemplate.findAll(Questionnaire.class);
        return list;
    }

    //查询用户是否答过题
    @Override
    public UserReport queryIsLock(Long userId, String wid) {

        Query query = new Query(Criteria.where("userId").is(userId).and("wid").is(wid));
        UserReport result = mongoTemplate.findOne(query, UserReport.class);
        return result;
    }

    @Override
    public List<Questions> queryQuestions(String id) {
        Query query = new Query(Criteria.where("wid").is(id));
        List<Questions> list = mongoTemplate.find(query, Questions.class);
        return list;
    }

    @Override
    public List<Options> queryOptions(String qid) {
        Query query = new Query(Criteria.where("qid").is(qid));
        List<Options> options = mongoTemplate.find(query, Options.class);
        return options;
    }


    @Override
    public Integer queryScore(String optionId) {
        Query query = new Query(Criteria.where("id").is(optionId));

        Options one = mongoTemplate.findOne(query, Options.class);
        Integer score = one.getScore();
        return score;
    }

    @Override
    public Questions queryQuestion(String questionId) {
        Query query = new Query(Criteria.where("id").is(questionId));
        Questions questions = mongoTemplate.findOne(query, Questions.class);
        return questions;
    }

    @Override
    public List<String> queryReport() {
        List<Report> reports = mongoTemplate.findAll(Report.class);
        ArrayList<String> result = new ArrayList<>();
        for (Report report : reports) {
            String id = String.valueOf(report.getId());
            result.add(id);
        }
        return result;

    }

    @Override
    public UserReport saveUserReport(Long userId, String wid, String report) {
        UserReport userReport = new UserReport();
        userReport.setUserId(userId);
        userReport.setRid(report);
        userReport.setWid(wid);

        UserReport result = mongoTemplate.insert(userReport);
        return result;
    }

    @Override
    public Report queryResults(String id) {
        Query query = new Query(Criteria.where("id").is(id));
        Report report = mongoTemplate.findOne(query, Report.class);
        return report;
    }

    @Override
    public String findQuestionnaireId(String questionId) {
        Questions questions = this.queryQuestion(questionId);
        String wid = String.valueOf(questions.getWid());
        Query query = new Query(Criteria.where("id").is(wid));
        Questionnaire questionnaire = mongoTemplate.findOne(query, Questionnaire.class);
        String QuestionnaireId = questionnaire.getId();
        return QuestionnaireId;
    }

    @Override
    public UserReport queryUserResults(String id) {
        Query query = new Query(Criteria.where("id").is(id));
        UserReport userReport = mongoTemplate.findOne(query, UserReport.class);
        return userReport;
    }

    //查询问卷类型
    @Override
    public Questionnaire queryQuestionnaireType(String wid) {
        Query query = new Query(Criteria.where("id").is(wid));
        Questionnaire questionnaire = mongoTemplate.findOne(query, Questionnaire.class);
        return questionnaire;
    }
}
